package com.company.cloud.user.modules.app.controller;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.constant.ErrorUserEnum;
import com.company.cloud.user.constant.UserConstant;
import com.company.cloud.user.constant.UserMsgSendTypeEnum;
import com.company.cloud.user.modules.app.controller.req.CompanyListByMobile2CodeParam;
import com.company.cloud.user.modules.app.controller.req.UserLoginByTokenParam;
import com.company.cloud.user.modules.app.models.UserCompanyModel;
import com.company.cloud.user.modules.app.service.UserAppService;
import com.company.cloud.user.modules.app.service.UserCodeService;
import com.company.cloud.user.modules.app.service.UserCompanyService;
import com.company.cloud.user.modules.app.service.UserLoginService;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.bus.producer.UserProducer;
import com.company.cloud.user.modules.cache.service.UserCompanyCheckService;
import com.company.cloud.user.modules.cache.service.UserPermissionCacheService;
import com.company.common.server.config.cloud.ProjectConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 令牌登录
 * */
@Log4j2
@RestController
@RequestMapping("/app/user/login")
@Api(tags = "UC_用户令牌登录")
public class UserLogin2TokenController {

    @Autowired
    private UserCodeService userCodeService;
    @Autowired
    private UserAppService userAppService;
    @Autowired
    private UserCompanyService userCompanyService;
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private UserPermissionCacheService userPermissionCacheService;
    @Autowired
    private ProjectConfig projectConfig;
    @Autowired
    private UserProducer userProducer;


    @PostMapping("/getCompanyListByMobile2Code")
    @ApiOperation(value = "(手机号+验证码)获取公司列表" ,response = UserCompanyModel.class)
    public BaseResult getCompanyListByMobile2Code(@RequestBody CompanyListByMobile2CodeParam params) {
        ValidatorUtils.validate(params);
        userCodeService.validate(UserConstant.Platform_VCode_Com_Def,
                UserMsgSendTypeEnum.Login_VCode, params.getMobile(), params.getVcode(), false);
        UserCompanyModel data= userAppService.getUserCompany(params.getMobile());
        return BaseResult.ok(data);
    }


    @PostMapping("/loginByToken")
    @ApiOperation(value = "(令牌)登录" ,response = LoginUserInfo.class)
    public BaseResult loginByToken(@RequestBody UserLoginByTokenParam params) {
        ValidatorUtils.validate(params);
        String mobile=userAppService.getMobileByToken(params.getToken(), true);
        UserInfo userInfo = userLoginService.getUserById(params.getUserId());
        if(!params.getCompanyNo().equals(userInfo.getCompanyNo())) {
            throw new BusinessException(ErrorUserEnum.User_Info_Null);
        }
        if(!mobile.equals(userInfo.getMobile())) {
            throw new BusinessException(ErrorUserEnum.User_Info_Null);
        }
        LoginUserInfo data= userLoginService.login(userInfo, null);
        userPermissionCacheService.loadPermissionRoleCache(data.getCompanyNo());
        userProducer.userLogin("令牌登录",params,data);
        return BaseResult.ok(data);
    }



}
